/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.qiujuer.genius.ui.widget;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.ComponentState;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import net.qiujuer.genius.ui.AttrValue;
import net.qiujuer.genius.ui.utils.AttrUtils;
import net.qiujuer.genius.ui.utils.ColorListHelper;

/**
 * This is TextField widget
 * The widget extend ohos.agp.components.TextField
 *
 * @since 2021-05-11
 */
public class TextField extends ohos.agp.components.TextField {
//    private Context context;
    private AttrSet attrSet;
    private RgbColor GAY_DEFAULT_COLOR = RgbColor.fromArgbInt(0x8A000000);
    private RgbColor BLUE_SELECTED_COLOR = RgbColor.fromArgbInt(0x00bcd4);
//    private RgbColor CURSOR_DEFAULT_COLOR = RgbColor.fromArgbInt(0x00bcd4);
    private String DEFAULT_COLOR = "#8A000000";

    /**
     * TextField
     *
     * @param context
     */
    public TextField(Context context) {
        this(context, null);
    }

    /**
     * TextField
     *
     * @param context
     * @param attrSet
     */
    public TextField(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * TextField
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public TextField(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
//        this.context = context;
        this.attrSet = attrSet;
        init();
    }

    private void init() {
        initTextField();
    }

    private void initTextField() {
        setEmptyColor(null);
        setCursorColor();
    }

    private void setCursorColor() {
        ShapeElement shapeElement = new ShapeElement();
        Color cursorColor = AttrValue.getColorValue(attrSet, "cursor_color", DEFAULT_COLOR);
        RgbColor rgbColor = RgbColor.fromArgbInt(cursorColor.getValue());
        shapeElement.setRgbColor(rgbColor);
        setCursorElement(shapeElement);
    }

    /**
     * setEmptyColor 设置控件默认颜色
     *
     * @param text
     */
    public void setEmptyColor(Text text) {
        StateElement basementStateElement = getStateElement("gray_basement");

        RgbColor normalRgbColor = getRgbColor(basementStateElement, new int[]{ComponentState.COMPONENT_STATE_EMPTY});

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(normalRgbColor);

        setBasement(shapeElement);

        Color color = new Color(Color.rgb(normalRgbColor.getRed(), normalRgbColor.getGreen(), normalRgbColor.getBlue()));
        setHintColor(color);

        if (text != null) {
            text.setTextColor(color);
        }
    }

    public void setFocusedColor(Text text) {
        StateElement basementStateElement = getStateElement("gray_basement");

        RgbColor focusedRgbColor = getRgbColor(basementStateElement, new int[]{ComponentState.COMPONENT_STATE_FOCUSED});

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(focusedRgbColor);

        setBasement(shapeElement);

        Color color = new Color(Color.rgb(focusedRgbColor.getRed(), focusedRgbColor.getGreen(), focusedRgbColor.getBlue()));
        setHintColor(color);

        if (text != null) {
            text.setTextColor(color);
        }
    }

    private StateElement getStateElement(String key) {
        StateElement basementStateElement = AttrUtils.get(attrSet, key,
            ColorListHelper.makeStateElement(GAY_DEFAULT_COLOR, BLUE_SELECTED_COLOR));
        return basementStateElement;
    }

    private RgbColor getRgbColor(StateElement stateElement, int[] stateArray) {
        RgbColor rgbColor = ColorListHelper.getColorByState(stateElement, stateArray);
        return rgbColor;
    }
}
